<?php
namespace api\controllers;

use Yii;
use yii\log\FileTarget;
class ErrorController extends \yii\web\Controller
{

    public function actionIndex()
    {

        $error = Yii::$app->errorHandler->exception;

        //错误存在记录日志
        if ($error)
        {
            $code = $error->getCode();
            $msg = $error->getMessage();
            $file = $error->getFile();
            $line = $error->getLine();


            $time = microtime(true);
            $log = new FileTarget();

            #日志文件目录
            $log->logFile = Yii::$app->getRuntimePath() . '/logs/err.log';

            #日志文件格式
            $err_msg = $msg . " [file: {$file}][line: {$line}][err code:$code.][url:{$_SERVER['REQUEST_URI']}][post:".http_build_query($_POST)."]";

            $log->messages[] = [
                $err_msg,
                1,
                'application',
                $time
            ];

            $log->export();

        }

        #重定向至首页
        return 'error';
    }
   
}